<template>
  <div>
    <!-- 搜索栏 -->
    <div>
      <el-input
        placeholder="请输入姓名"
        v-model="input"
        clearable
        style="width: 300px; margin: 0 10px 20px 0"
      >
      </el-input>
      <el-button type="primary" size="medium" @click="adminGl">搜索</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; text-align: center"
      @selection-change="handleSelectionChange"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55"> </el-table-column>

      <el-table-column prop="name" label="角色名称"></el-table-column>

      <el-table-column label="账号">
        <template slot-scope="scope">{{ scope.row.email }}</template>
      </el-table-column>

      <el-table-column label="手机号">
        <template slot-scope="scope">{{ scope.row.phone }}</template>
      </el-table-column>

      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <el-switch
            style="display: block"
            v-model="scope.row.checkbutton"
            inactive-color="#13ce66"
            active-text="超级管理员"
            inactive-text="普通用户"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="添加时间">
        <template slot-scope="scope">{{ scope.row.yuyue }}</template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
        style="margin: 20px 0; float: right"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { postAdminGlnameApi } from "@/api/adminApi/gl/adminGlApi";
import { mapGetters, mapMutations } from "vuex";
export default {
  data() {
    return {
      tableData: [
        {
          name: "超级管理员",
          address: "有最高权限的角色,不可删除",
          date: "2021-11-11",
        },
        {
          name: "普通用户",
          address: "普通用户,不可删除",
          date: "2021-11-11",
        },
      ],
      multipleSelection: [],
      input: "",
      // 分页
      currentPage: 1,
      checkbutton: false,
    };
  },
  // 渲染
  async mounted() {
    let bloon = await this.$store.dispatch("admin/listFn");
    // console.log("gl管理员", bloon);
    // console.log("vuex获取的list",this.list);
    this.tableData = this.list;
  },
  methods: {
    // 查
    async adminGl() {
      let finds = await postAdminGlnameApi(this.input);
      // console.log("finds", finds);
      this.tableData = [];
      this.tableData.push(finds);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 改
    handleEdit(index, row) {
      // console.log(index, row);
      this.$store.commit("admin/listedit", row);
    },
    // 删
    handleDelete(index, row) {
      // console.log("del", index, row, row.id, this.list);
      this.$store.commit("admin/listdel", index);
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
    },
  },
  computed: {
    ...mapGetters(["list"]),
  },
};
</script>

<style lang="scss" scoped>
@import "./scss/index.scss";
</style>